package com.forage.exp.controller;

import com.forage.exp.po.Response;
import com.forage.exp.po.User;
import com.forage.exp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.iceclean.logtrace.annotation.EnableLogTrace;
import top.iceclean.logtrace.bean.Logger;

/**
 * @author : George
 * @date : 2022-05-3
 */
@EnableLogTrace
@RestController
@RequestMapping("/user")
public class UserController {

    private Logger logTrace;

    @Autowired
    private UserService userService;

    /**
     * 用户登陆
     * @param account   用户账号
     * @param password  用户密码
     * @return  用户名与相应JWT码
     */
    @PostMapping("/login")
    public Response login(@RequestParam("account") String account,
                          @RequestParam ("password") String password){
        return userService.login(User.builder()
                .userAccount(account)
                .userPassword(password)
                .build());
    }

    /**
     * 管理员查询用户信息
     * @param account 所查询用户账号
     * @return 所查询用户信息
     */
    @GetMapping("/getOneByUserAccount/{account}")
    @PreAuthorize("hasAuthority('Admin')")
    public Response getOneByUserAccount(@PathVariable("account") String account) throws IllegalAccessException {
        return userService.getOneByUserAccount(account);
    }

}
